package com.yubest.mqtt.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;

/**
 * 消息接收器
 *
 * @Author: hweiyu
 * @Date: 2022/11/11 9:28
 */
@Slf4j
public class MqttConsumer implements MessageHandler {

    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        try {
            MessageHeaders headers = message.getHeaders();
            //接收到的消息的topic
            String receivedTopic = (String) headers.get(MqttHeaders.RECEIVED_TOPIC);
            log.info("接收到的消息的topic：{}", receivedTopic);
            //接收到的消息体
            String payload = (String) message.getPayload();
            log.info("接收到的消息体payload：{}", payload);
        } catch (Exception e) {
            log.error("处理消息失败", e);
        }
    }
}
